package com.android.openapp.ui;

import com.android.openapp.R;
import com.android.openapp.data.Place;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ShowInfoDialog extends Dialog {

	Context mContext;
	int listIndex;
	Button infoButton;
	DialogInfoButtonListener mInfoButtonListener;
	
	public ShowInfoDialog(Context context, int screenId, int buttonTextId, int placeListIndex) {
		super(context,android.R.style.Theme_NoTitleBar);
		mContext = context;
		listIndex = placeListIndex;
		
		setCancelable(true);
		
		//inflate the layout
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(screenId, null);
		
        setContentView(layout);
        
        Place place = Place.lista.get(listIndex);
		//setTitle(place.getDenominazione());
        TextView denominazione = (TextView) layout.findViewById(R.id.denominazione);
        denominazione.setText(place.getDenominazione());
        
		//UI elements
        TextView tipo = (TextView)layout.findViewById(R.id.tipo);
        TextView indirizzo = (TextView)layout.findViewById(R.id.indirizzo);
        tipo.setText(place.getTipologia());
        indirizzo.setText(place.getAddress());
        TextView contatti = (TextView) layout.findViewById(R.id.contatti);
        String cInfo = place.getContactInfo();
        if(cInfo != null)
        	contatti.setText(cInfo);
        
		//coordinate
		final Double lat = place.getLatitude();
		final Double lon = place.getLongitude();
		
		//bottone per passare alla mappa
        Button mapButton = (Button) layout.findViewById(android.R.id.button1);
        mapButton.setText(buttonTextId);
        mapButton.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				if(lat != null && lon != null){
					mInfoButtonListener.onButtonClicked(listIndex, lat, lon);
					dismiss();
				}
				else {
					String text = mContext.getResources().getString(R.string.noaddressfound);
					Utils.showToast(mContext, text);
				}
			}
        	
        });
	}
	
	/**
	 * 
	 * @param listener Listener personalizzabile per definire il comportamento del bottone
	 */
	public void setInfoButtonListener(DialogInfoButtonListener listener){
		mInfoButtonListener = listener;
	}
}
